package main.scala.thread

object ScalaThreadLab {
  /**
   * 起始时间
   */
  var startTime: Long = 0L

  /**
   * 主方法
   * @param args 命令参数
   */
  def main(args: Array[String]): Unit = {

    MyArray.init()
    val threadSize = MyArray.target

    // 并行测试
    startTime = System.nanoTime()
    for (_ <- 0 until threadSize) {
      val thread = new Thread(new MyTask())
      thread.start()
    }

    // 等待其他线程结束
    Thread.sleep(5 * 1000)

    // 串行测试
    val newStartTime = System.nanoTime()
    for (i <- 0 until threadSize) {
      task()
    }
    println("串行测试用时：" + (System.nanoTime - newStartTime))

  }

  /**
   * 任务方法
   */
  def task(): Unit = {
    var sum = 0
    for (i <- MyArray.arr.indices) {
      sum += MyArray.arr(i) * 10 / 7
    }
  }

  /**
   * 打印并行测试时间
   */
  def printTime(): Unit = {
    println("并行测试用时：" + (System.nanoTime - startTime))
  }
}

